package com.wyg.demo.component;

import com.wyg.demo.exception.ServiceException;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ygWen
 * @Description: 文件操作类
 * @date 2021/12/29
 */
@Component
public class FileOperation {
    /**
     * 获得指定文件夹中的所有csv文件的绝对路径
     * @param dirPath 文件夹路径
     * @return csv文件list集合
     */
    public List<String> getCsvFileOnDir(String dirPath){
        File file = new File(dirPath);
        File[] files = file.listFiles();
        List<String> csvFilelist = new ArrayList<>();
        if (files != null) {
            for (File f : files) {
                if (f.isFile()){
                    String fileName = f.getName();
                    if ("csv".equals(fileName.substring(fileName.lastIndexOf(".") + 1))){
                        String absolutePath = f.getAbsolutePath();
                        csvFilelist.add(absolutePath);
                    }
                }
            }
        }
        return csvFilelist;
    }

    /**
     * 向文件写入内容
     * @param dest 目标文件
     * @param content 要写入的内容
     */
    public void writeFile(String dest, String content) {

        FileOutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            File file = new File(dest);
            if (!file.exists()) {
                file.createNewFile();
            }

            fileOutputStream = new FileOutputStream(file, true);

            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferedWriter.write(content);
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 一行一行的读取文件中的内容
     * @param filePath 文件路径
     * @return 读取到的内容list
     */
    public List<String> readFile(String filePath) {
        FileInputStream fileInputStream = null;
        BufferedReader bufferedReader = null;
        List<String> list = new ArrayList<>();
        try {
            File file = new File(filePath);
            if (!file.exists()){
                file.createNewFile();
            }
            fileInputStream = new FileInputStream(file);
            bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line = null;
            while ((line = bufferedReader.readLine()) != null){
                list.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
